package org.example;


import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class Test2 {
    //leetcode 102 二叉树的层序遍历 https://leetcode.cn/problems/binary-tree-level-order-traversal/description/
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ret = new ArrayList<>();
        if (root == null) return ret;
        Deque<TreeNode> deque = new ArrayDeque<>();
        deque.add(root);
        int count = 0;
        while (!deque.isEmpty()) {
            List<Integer> list = new ArrayList<>();
            count = deque.size();
            while (count > 0) {
                TreeNode tmp = deque.poll();
                if (tmp.left != null) deque.add(tmp.left);
                if (tmp.right != null) deque.add(tmp.right);
                list.add(tmp.val);
                count--;
            }
            ret.add(list);
        }
        return ret;
    }
}
